package com.sevlet;

import com.alipay.api.AlipayResponse;
import com.alipay.api.domain.TradeFundBill;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.MonitorHeartbeatSynResponse;
import com.alipay.demo.trade.DemoHbRunner;
import com.alipay.demo.trade.Main;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sevlet.BaseSevlet;
import com.util.JsonUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

@WebServlet("/AliPaySevlet")
public class AliPaySevlet extends BaseSevlet {
    //生成二维码
    public void BuildEwm(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        String oderId = req.getParameter("OderId");
        String totalPrice = req.getParameter("TotalPrice");
        String goodsName = req.getParameter("GoodsName");
        Main main = new Main();
        Gson gson = new GsonBuilder().create();
        JsonUtil jsonUtil = new JsonUtil();
        try {
            main.test_trade_precreate(oderId,totalPrice,goodsName);
            jsonUtil.setCode(0);
            jsonUtil.setMsg("生成二维码成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg(e.getMessage());
        }finally {
            writer.write(gson.toJson(jsonUtil));
        }


    }
    //查询订单是否完成
    public void QueryPayState(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        String oderId = req.getParameter("OderId");
        Main main = new Main();
        Gson gson = new GsonBuilder().create();
        JsonUtil jsonUtil = new JsonUtil();
        try {
            main.test_trade_query(oderId);
            jsonUtil.setCode(0);
            jsonUtil.setMsg("支付成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg(e.getMessage());
        }finally {
            writer.write(gson.toJson(jsonUtil));
        }
    }


}
